package com.matt.controller.pad;

import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.Notice.Notice;
import com.matt.foundation.constant.PermissionConstant;
import com.matt.foundation.constant.RequestConstant;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.ModelUtil;
import com.matt.service.NoticeService;
import com.matt.service.remoteCall.SsoCallService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.matt.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/pad/notice")
@Slf4j
public class PadNoticeController {

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private SsoCallService ssoCallService;

    /**
     * 废弃
     * @param ticket
     * @param title
     * @param content
     * @param noticeType
     * @return
     */
    @RequestMapping("/addNotice")
        public StatusResponse addNotice(@RequestHeader(TICKET) String ticket, String title, String content, Integer noticeType){
        if(null == noticeType){
            noticeType = 1 ;
        }
        Notice notice = noticeService.addNotice(title, content, noticeType, cache.getStaffId(ticket), cache.getAreaId(ticket));
        return StatusResponse.success(notice);
    }



    @RequestMapping("/getNoticeList")
    public StatusResponse getNoticeListByAreaId(@RequestHeader(TICKET) String ticket,Integer noticeType, PagerQuery query){
        Page<Notice> recordPage = noticeService.getNoticeListByAreaId(cache.getAreaId(ticket), noticeType, query.getPageNum(), query.getPageSize());
        return StatusResponse.success(recordPage);
    }


    @RequestMapping("/getNoticeListByTicket")
    public StatusResponse getNoticeListByTicket(@RequestHeader(TICKET) String ticket, PagerQuery query){
        Page<Notice> recordPage = null ;

        if (ssoCallService.checkStaffPermission(ticket, new String[]{PermissionConstant.TEACH_NOTICE_FULL})){
            recordPage = noticeService.getNoticeListByAreaId(cache.getAreaId(ticket), null, query.getPageNum(), query.getPageSize());
        } else {
            recordPage = noticeService.getNoticeListByStaffId(cache.getStaffId(ticket),cache.getAreaId(ticket), null, query.getPageNum(), query.getPageSize());
        }

        return StatusResponse.success(recordPage);
    }



    @RequestMapping("/deleteNotice")
    public StatusResponse deleteNotice(@RequestHeader(RequestConstant.TICKET) String ticket, String noticeId) {
        Notice notice =  noticeService.deleteNotice(ticket, noticeId);
        return StatusResponse.success(ModelUtil.mapOf("noticeId", notice.getId(), "deleteStatus", notice.getDeleted()));
    }


    @RequestMapping("/addNoticeByClasses")
    public StatusResponse addNoticeByClasses(@RequestHeader(TICKET) String ticket, String title, String content, Integer noticeType,String classesIds){
        if(null == noticeType){
            noticeType = 1 ;
        }
        if(StringUtils.isBlank(classesIds))
            throw new BusinessException(ErrorCode.PARAM_CAN_NOT_BLANK,"需要传班级！");

        String [] classesArray = classesIds.split(",");
        Notice notice = noticeService.addNoticeByClasses(title, content, noticeType, cache.getStaffId(ticket), cache.getAreaId(ticket), classesArray);
        return StatusResponse.success(notice);
    }


    @RequestMapping("/noticeDetail")
    public StatusResponse noticeDetail(@RequestHeader(RequestConstant.TICKET) String ticket, String noticeId) {
        Notice notice =   noticeService.noticeDetail(noticeId);
        return StatusResponse.success(notice);
    }



}
